package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.R;
import com.tiantianyuegou.http.ApiDefine;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * webview
 */
public class WebActivity extends BaseActivity implements View.OnClickListener {

    private ImageView img_title;
    private TextView text_title, text_menu;
    private WebView web;
    private String web_select = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_web);
       Tools.changeSystemBar(this);


        findViewByIds();
        initListener();
        initWebView();
    }

    /**
     * 初始化webView
     */
    private void initWebView() {
        web.getSettings().setJavaScriptEnabled(true);
        // 一直在本页面打开
        web.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
        // 更新进度条
        web.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                //mTxt_title.setText(title);
            }
        });
        web.setVerticalScrollBarEnabled(false);
        web.setHorizontalScrollBarEnabled(false);
        WebSettings settings = web.getSettings();
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
    }

    public void findViewByIds() {
        img_title = (ImageView) findViewById(R.id.img_title);
        text_title = (TextView) findViewById(R.id.text_title);
        text_menu = (TextView) findViewById(R.id.text_menu);
        web = (WebView) findViewById(R.id.web);

        Intent web_intent = getIntent();
        web_select = web_intent.getStringExtra("type");

        if (web_select.equals("3")) {
            text_title.setText(R.string.web_title_us);
            getProtocolDta("3");
        } else if (web_select.equals("2")) {
            text_title.setText(R.string.web_title_use);
            getProtocolDta("2");
        } else if (web_select.equals("CategoryAdv")) {
            text_title.setText("首页广告");
            Log.e("TAG", getIntent().getStringExtra("url"));
            web.loadUrl(getIntent().getStringExtra("url"));
        } else if ("4".equals(web_select)) {
            text_title.setText(R.string.web_title_register);
            getProtocolDta("4");
        }

    }

    private void getProtocolDta(String s) {
        final LoadingDialog loadingDialog = new LoadingDialog(this);
        loadingDialog.setMsg("正在请求数据...");
        loadingDialog.show();
        RequestParams params = new RequestParams();
        params.addBodyParameter("cateId", s);
        Log.e("TAG","get protocol params:"+s);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.PROTOCOL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                Log.e("TAG", "get protocol data response:" + responseInfo.result);
                if (TextUtils.isEmpty(responseInfo.result)){
                    showMsg("获取数据失败");
                }else{
                    try {
                        JSONObject root=new JSONObject(responseInfo.result);
                        String code=root.getString("code");
                        if("100".equals(code)){
                            JSONObject value=root.getJSONObject("result");
                            String content=value.getString("tiContent");
                            web.loadDataWithBaseURL(null,content,"text/html", "utf-8",null);
                        }else{
                            String msg=root.getString("message");
                            showMsg(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        showMsg("解析异常");
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
                showMsg("当前网络不可用,请检查您的网络设置");
                Log.e("TAG", "get protocol data failure:" + s);
            }
        });
    }


    public void initListener() {
        img_title.setOnClickListener(this);
    }


    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title:
                finish();
                break;

            default:
                break;
        }

    }
}
